package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    public List<AgreementItemVo> findAgreementItemVoByPhone(String phone) {
        return leaseAgreementMapper.findAgreementItemVoByPhone(phone);
    }

    @Override
    public AgreementDetailVo findAgreementDetailVoById(Long id) {
        LeaseAgreement leaseAgreement = this.getById(id);
        //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        //查询图片信息
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectGraphVoList(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        //查询房间号
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        //查询房间图片列表
        List<GraphVo> graphVoList= graphInfoMapper.selectGraphVoListByRoomId(ItemType.ROOM,id);
        //查询支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        //查询租期信息
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(graphVoList);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        return agreementDetailVo;
    }
}




